-
Notifications
You must be signed in to change notification settings - Fork 30
isNull, isUndefined convenience functions #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
a0ad924
to
f0816da
Compare
this could be simplified with v11 thanks to the new representation of untagged variants that will allow module Nullable = {
@unboxed type t<'a> = Present('a) | @as(null) Null | @as(undefined) Undefined
} Then those utility functions could be replaced with a simple pattern matching. |
It's not related to the post, but I've just thought of a possible problem that So maybe we should recover the @cristianoc's idea from some time ago? https://forum.rescript-lang.org/t/rfc-automatic-optional-chaining/3791/17?u=dzakh |
That idea amounts to accepting that nested nullable is the same as nullable. |
Even if pattern matching can be used, I still think having boolean-returning convenience functions as in this PR are useful because they are easily discoverable and can sometimes be used for more concise code than pattern matching. I don't have my code in front of me but know I have often used |
yeah I agree, those utility functions can come handy from time to time (eg as a parameter of a filter function). |
I was writing some code checking for null and undefined and found it awkward. Originally I tried
%raw
which worked but wasn't pretty. I tried a bunch ofNullable.toOption
and then checked forNone
and that was a mess. Then I realized I could use the==
operator, but that also wasn't pretty. There was the uncertainty of do I use==
or===
. In JavaScriptnull==undefined
I think. The worst is if you are checking if something is null or undefined...So here are some convenience functions like
Nullable.isNull
andNullable.isUndefined
that are analogous toOption.isNone
andOption.isSome
that make the code more readable. Includes tests and docs.